package com.zatech.cgnci.project.controller.claim;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zatech.cgnci.project.base.context.UserContext;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.dto.CgnciClaimCaseCheckDTO;
import com.zatech.cgnci.project.dto.CgnciClaimReserveDTO;
import com.zatech.cgnci.project.dto.ReserveDetailQueryDTO;
import com.zatech.cgnci.project.entity.CgnciClaimReserveTaskDO;
import com.zatech.cgnci.project.entity.UserEntity;
import com.zatech.cgnci.project.mapper.CgnciClaimReserveTaskMapper;
import com.zatech.cgnci.project.service.CgnciClaimReserveHistoryService;
import com.zatech.cgnci.project.service.CgnciClaimReserveService;
import com.zatech.cgnci.project.service.CgnciReinsuranceInfoService;
import com.zatech.cgnci.project.service.IApprovalService;
import com.zatech.cgnci.project.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/api/claim/reserve/task")
@RequiredArgsConstructor
@Api(produces = "application/json", tags = "理赔准备金计提管理api接口文档")
public class CgnciClaimReserveTaskController {

    @Autowired
    private CgnciClaimReserveService claimReserveService;
    @Autowired
    private IApprovalService approvalService;
    @Autowired
    private CgnciClaimReserveTaskMapper reserveTaskMapper;
    @Autowired
    private CgnciReinsuranceInfoService reinsuranceInfoService;
    @Autowired
    private CgnciClaimReserveHistoryService claimReserveHistoryService;

    @GetMapping("/detail")
    @ApiOperation(value = "详情")
    public CgnciClaimReserveVO detail(@RequestParam(value = "caseNo") @NotNull(message = "caseNo不能为空") String caseNo,
                                      @RequestParam(value = "isSendBack", required = false) String isSendBack) {
        return claimReserveService.detail(caseNo, isSendBack);
    }

//    @GetMapping("/detailTask")
//    @ApiOperation(value = "审批任务详情")
//    public CgnciClaimReserveTaskVO detailTask(@RequestParam(value = "taskNo") @NotNull(message = "taskNo不能为空") String taskNo) {
//        return claimReserveService.detailTask(taskNo);
//    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "新增修改审核")
    @Transactional(rollbackFor = Exception.class)
    public String saveOrUpdate(@RequestBody @Valid CgnciClaimReserveDTO updateDTO) {
        return claimReserveService.saveOrUpdateReserve(updateDTO);
    }

    @GetMapping("/verify")
    @ApiOperation(value = "计提调整校验接口")
    public void verify(@RequestParam(value = "caseNo") @NotNull(message = "caseNo不能为空") String caseNo) {
        claimReserveService.verify(caseNo);
    }

    @PostMapping("/approve")
    @ApiOperation(value = "审核流程任务")
    @Transactional(rollbackFor = Exception.class)
    public void approve(@RequestBody @Valid CgnciClaimCaseCheckDTO flowTaskDTO) {
        claimReserveService.approve(flowTaskDTO);
    }

    @GetMapping("/listBackNode")
    @ApiOperation(value = "获取退回流程节点")
    @Transactional(rollbackFor = Exception.class)
    public List<ApprovalConfigVO> listBackNode(@RequestParam(value = "taskNo") @NotNull(message = "taskNo不能为空") String taskNo) {
        CgnciClaimReserveTaskDO reserveTaskDO = reserveTaskMapper.selectOne(new LambdaQueryWrapper<CgnciClaimReserveTaskDO>().eq(CgnciClaimReserveTaskDO::getTaskNo, taskNo));
        return approvalService.listBackNode(taskNo, taskNo);
    }

    @PostMapping("/back")
    @ApiOperation(value = "退回流程任务")
    @Transactional(rollbackFor = Exception.class)
    public void back(@RequestBody @Valid CgnciClaimCaseCheckDTO flowTaskDTO) {
        claimReserveService.back(flowTaskDTO);
    }

    @GetMapping("/signOut")
    @ApiOperation(value = "注销")
    @Transactional(rollbackFor = Exception.class)
    public void signOut(@RequestParam(value = "taskNo") @NotNull(message = "taskNo不能为空不能为空") String taskNo) {
        claimReserveService.cancel(taskNo);
    }

    @GetMapping("/getRisks")
    @ApiOperation(value = "获取险种")
    public CgnciReinsuranceInfoVO getRisks(@RequestParam(value = "insuranceId") @NotNull(message = "insuranceId不能为空不能为空") Long insuranceId) {
        return reinsuranceInfoService.getReinsuranceInfo(insuranceId);
    }

    @GetMapping("/log")
    @ApiOperation(value = "获取操作日志")
    public List<ApprovalRecordVO> log(@RequestParam(value = "caseId") @NotNull(message = "caseId不能为空") Long caseId) {
        return claimReserveService.log(caseId);
    }


    @PostMapping("/reserveDetail")
    @ApiOperation(value = "准备金历史详情")
    public IPage<CgnciClaimReserveHistoryPageVO> reserveDetail(@RequestBody @Valid ReserveDetailQueryDTO req) {
        //return claimReserveService.reserveDetail(req);
        return claimReserveHistoryService.pageReserveHistory(req);
    }
}
